package com.clip.service;

import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clip.dao.SystemDateDAO;
import com.clip.entity.SystemDate;

@Service
@Transactional
public class SystemDateService {
	@Autowired
	private SystemDateDAO systemDateDAO;
	
	public List<SystemDate> getSystemHoliday(Integer year){
		
		String hql="FROM SystemDate systemDate where systemDate.year="+year;
		List<SystemDate> systemDateList = systemDateDAO.findByQuery(hql);
		return systemDateList;
		
	}
	
	public Map<String,Object> orgSystemHolidayList(List<SystemDate> list){
		Map<String,Object> infoMap=new LinkedHashMap<String,Object>();
		for(SystemDate systemDate : list){
			Map<String,Object> userDateMap=new LinkedHashMap<String,Object>();
			userDateMap.put("year", systemDate.getYear());
			userDateMap.put("month", systemDate.getMonth());
			userDateMap.put("day", systemDate.getDay());
			userDateMap.put("lastday", systemDate.getLastdays());
			userDateMap.put("name", systemDate.getName());
			infoMap.put("userdate id="+systemDate.getId(), userDateMap);
		}
		return infoMap;
	}
	
	public int getSystemHolidayByDay(int year,int month,int day){
		String hql="FROM SystemDate systemDate where systemDate.year="+year+" and systemDate.month="+month+" and systemDate.day="+day;
		List list=systemDateDAO.findByQuery(hql);
		if(list==null||list.size()==0){
			return -1;
		}else{
			SystemDate date=(SystemDate) list.get(0);
			if(date.getDatestatus()==0)
				return 0;
			else 
				return 1;
		}
	}
	/*
	public List<SystemDate> getSystemHolidayByDay(int year,int month,int day){
		String hql="FROM SystemDate systemDate where systemDate.year="+year+" and (systemDate.month="+month+" or systemDate.month="+(month+1)+" )";
		List<SystemDate> systemDateList = systemDateDAO.findByQuery(hql);
		for (Iterator iterator = systemDateList.iterator(); iterator.hasNext();) {
			SystemDate systemDate = (SystemDate) iterator.next();
			Calendar start=Calendar.getInstance();
			start.set(systemDate.getYear(), systemDate.getMonth()-1, systemDate.getDay());
			Calendar end=(Calendar) start.clone();
			end.add(Calendar.DATE,systemDate.getLastdays()-1);
			Calendar temp=(Calendar) start.clone();
			temp.set(year, month,day);
			if(start.getTimeInMillis()<=temp.getTimeInMillis()&&temp.getTimeInMillis()<=end.getTimeInMillis()){
				
			}else{
				iterator.remove();
			}
			
		}
		return systemDateList;
	}*/
}
